﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern.Admin
{
    public partial class EditArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        Facades mnarfacade = new Facades();
        //Load Data
        private void LoadData()
        {
            DropDownListParent.DataValueField = "MenuID";
            DropDownListParent.DataTextField = "Name";
            DropDownListParent.DataSource = mnarfacade.SelectParentID();
            DropDownListParent.DataBind();

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btClose.Click += btClose_Click;
            btReset.Click += btReset_Click;
            btSave.Click += btSave_Click;
        }
        //Reset form
        private void Reset()
        {
            txtTitle.Text = "";
            txtAlias.Text = "";
            CKEditorIntroText.Text = "";
            CKEditorFullText.Text = "";
            lblCreatedBy.Text = "";
            lblCreatedDate.Text = "";
            txtImgUrl.Text = "";
            lblCheckoutBy.Text = "";
            lblCheckoutDate.Text = "";
            lblHit.Text = "";
            chkPublished.Checked = false;
            lblModifiedBy.Text = "";
            lblModifiedDate.Text = "";
        }
        //Get Data
        private Articles GetData()
        {
            Articles data = new Articles();
            data.Title = txtTitle.Text;
            data.Alias = txtAlias.Text;
            data.IntroText = CKEditorIntroText.Text;
            data.FullText = CKEditorFullText.Text;
            data.CreateBy = User.Identity.Name;
            data.CreateDate = DateTime.Now;
            data.ModifeldBy = lblModifiedBy.Text;
            data.DemoUrl = txtImgUrl.Text;
            data.State = true;
            data.Publisher = chkPublished.Checked;
            data.Hits = 0;
            data.CheckoutBy = lblCheckoutBy.Text;
            data.CheckoutDate = DateTime.Now;
            data.MenuID = Convert.ToInt32(DropDownListParent.SelectedValue);
            return data;
        }
        void btReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        void btClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("Article.aspx");
        }
        void btSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool result = false;
                Articles data = GetData();
                result = mnarfacade.AddArticle(data);
                lblStatus.Text = result ? "Add thành công!" : "Add không thành công!";
                if (result)
                {
                    Reset();
                }
            }
        }
    }
}